package com.wlyuan.open.scanner.domain.user;

import com.wlyuan.open.scanner.dao.entity.UserDO;
import com.wlyuan.open.scanner.domain.tenant.InviterInfo;
import com.wlyuan.open.scanner.utils.JsonUtils;
import com.wlyuan.open.scanner.utils.UserOpenIdUtils;
import com.wlyuan.open.scanner.utils.mappings.BaseMapping;
import com.wlyuan.open.scanner.utils.mappings.MappingInvalid;
import org.mapstruct.*;
import org.mapstruct.factory.Mappers;

import java.util.List;

/**
 * @author lijing
 * @description
 */
public class UserConvert {

    private static final Convert CONVERT = Mappers.getMapper(Convert.class);

    @Mapper(uses = {MappingInvalid.class, MappingUserRole.class, MappingStringToDate.class})
    public interface Convert {

        @Mappings({
                @Mapping(target = "id", source = "userId"),
                @Mapping(target = "name", source = "userName"),
                @Mapping(target = "type", source = "userType"),
                @Mapping(target = "mobile", source = "userPhone"),
                @Mapping(target = "email", constant = ""),
                @Mapping(target = "account", source = "userAccount"),
                @Mapping(target = "password", source = "userPassword"),
                @Mapping(target = "nick", source = "userNickname"),
                @Mapping(target = "gender", source = "userGender"),
                @Mapping(target = "birthday", source = "userBirthday"),
                @Mapping(target = "department", source = "userDept"),
                @Mapping(target = "position", source = "userDuty"),
                @Mapping(target = "nickName", source = "userNickname"),
                @Mapping(target = "portrait", source = "userPortrait"),
                @Mapping(target = "openId", source = "userOpenid"),

                @Mapping(target = "roles", source = "userRoleid"),

                @Mapping(target = "tenantId", source = "userCompanyid"),
                @Mapping(target = "sourceKey", source = "userId"),
                @Mapping(target = "createTime", source = "userInserttime"),
                @Mapping(target = "updateTime", source = "userUpdatetime"),
                @Mapping(target = "isAlias", source = "userIsdomain"),
        })
        @BaseMapping
        User convert(UserDO source);

        @AfterMapping
        default void after(@MappingTarget User target, UserDO source) {
            target.setRaw(JsonUtils.withNullJson(source));
            target.setOpenId(UserOpenIdUtils.createOpenId(target.getOpenId()));
        }

        List<User> convert(List<UserDO> list);

        @Mappings({
                @Mapping(target = "inviterMobile", source = "source.mobile"),
                @Mapping(target = "inviterName", source = "source.name"),
                @Mapping(target = "inviterUserId", source = "source.id"),
                @Mapping(target = "inviterTenantId", source = "source.tenantId"),
                @Mapping(target = "tenantId", source = "companyId"),
                @Mapping(target = "sourceKey", source = "companyId")
        })
        @BaseMapping
        InviterInfo convertInviter(User source, Long companyId);

        @AfterMapping
        default void after(@MappingTarget InviterInfo target, User source) {
            target.setRaw(JsonUtils.withNullJson(source));
        }

    }

    public static List<User> convert(List<UserDO> creation) {
        return CONVERT.convert(creation);
    }

    public static User convert(UserDO source) {
        return CONVERT.convert(source);
    }

    public static InviterInfo convertInviter(User source, Long companyId) {
        return CONVERT.convertInviter(source, companyId);
    }
}
